package com.forevershine.action;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.forevershine.business.context.ContextBuilder;
import com.forevershine.business.model.ActionNotes;
import com.forevershine.business.model.BodyFabric;
import com.forevershine.business.model.ColorSizeCurve;
import com.forevershine.business.model.StyleDetail;
import com.forevershine.business.model.Task;
import com.forevershine.business.model.Trim;
import com.forevershine.business.report.model.SampleNoteReport;
import com.forevershine.business.service.impl.ActionNotesServiceImpl;
import com.forevershine.business.service.impl.BodyFabricServiceImpl;
import com.forevershine.business.service.impl.SampleNoteReportServiceImpl;
import com.forevershine.business.service.impl.StyleDetailServiceImpl;
import com.forevershine.business.service.impl.TaskServiceImpl;
import com.forevershine.business.service.impl.TrimServiceImpl;
import com.forevershine.common.BusinessConstants;
import com.opensymphony.xwork2.ActionContext;

public class IssueNoteAction extends BaseAction {

	private static Log log = LogFactory.getLog(IssueNoteAction.class);

	private StyleDetail styleDetail = new StyleDetail();

	private BodyFabric bodyFabric = new BodyFabric();

	private Trim trim = new Trim();

	private ActionNotes actionNotes = new ActionNotes();

	private SampleNoteReport sampleNoteReport = new SampleNoteReport();

	private Task task = new Task();

	private ColorSizeCurve colorSizeCurve = new ColorSizeCurve();

	public ColorSizeCurve getColorSizeCurve() {
		return colorSizeCurve;
	}

	public void setColorSizeCurve(ColorSizeCurve colorSizeCurve) {
		this.colorSizeCurve = colorSizeCurve;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public SampleNoteReport getSampleNoteReport() {
		return sampleNoteReport;
	}

	public void setSampleNoteReport(SampleNoteReport sampleNoteReport) {
		this.sampleNoteReport = sampleNoteReport;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 19L;

	@Override
	protected String service() {
		
		if( checkTaskDetailFilled())
		{
			saveBasicInfo();
			
			fillReport();
			
			saveBaseReport();
		}

		return SUCCESS;
	}

	private boolean checkTaskDetailFilled() {
		return (task.getIndentNumber()!=null && task.getIndentNumber().trim()!="" && task.getStyleNumber()!=null && task.getStyleNumber()!="");
	}

	private void fillReport() {
		sampleNoteReport.setBrand(task.getBrand());
		sampleNoteReport.setUser("DemoUser");
		sampleNoteReport.setProductname("what product");
		sampleNoteReport.setIndentno(task.getIndentNumber());
		sampleNoteReport.setStyleno(task.getStyleNumber());
		sampleNoteReport.setStyle_detail_units(styleDetail.getTotalunits());
		sampleNoteReport.setWash_notes(actionNotes.getWash());
		sampleNoteReport.setTrims_notes(actionNotes.getTrims());
		sampleNoteReport.setFabric_notes(actionNotes.getFabric());
			
	}
	
	private void saveBasicInfo()
	{
		TaskServiceImpl taskSvc = ContextBuilder.getContext().getInstance(
				TaskServiceImpl.class);
		task.setCreationDate(new Date());
		taskSvc.insertTask(task);
		log.info(task.getTaskId());
		
		TrimServiceImpl trimSvc = ContextBuilder.getContext().getInstance(
				TrimServiceImpl.class);
		trim.setTaskId(task.getTaskId());
		trimSvc.insertTrim(trim);
		
		StyleDetailServiceImpl styleDetailSvc = ContextBuilder.getContext().getInstance(
				StyleDetailServiceImpl.class);
		styleDetail.setTaskid(task.getTaskId());
		styleDetailSvc.insertStyleDetail(styleDetail);
		
		BodyFabricServiceImpl bodyFabricSvc = ContextBuilder.getContext().getInstance(
				BodyFabricServiceImpl.class);
		bodyFabric.setTaskid(task.getTaskId());
		bodyFabricSvc.insertBodyFabric(bodyFabric);
		
		ActionNotesServiceImpl actionNotesSvc = ContextBuilder.getContext().getInstance(
				ActionNotesServiceImpl.class);
		actionNotes.setTaskid(task.getTaskId());
		actionNotesSvc.insertActionNotes(actionNotes);
		
		ActionContext.getContext().getSession().put(BusinessConstants.TASKID, task.getTaskId());
	}
	
	private void saveBaseReport()
	{
		SampleNoteReportServiceImpl sampleNoteReportSvc = ContextBuilder.getContext().getInstance(
				SampleNoteReportServiceImpl.class);
		sampleNoteReport.setTaskid(task.getTaskId());
		sampleNoteReportSvc.insertSampleNoteReport(sampleNoteReport);
	}

	public BodyFabric getBodyFabric() {
		return bodyFabric;
	}

	public void setBodyFabric(BodyFabric bodyFabric) {
		this.bodyFabric = bodyFabric;
	}

	public StyleDetail getStyleDetail() {
		return styleDetail;
	}

	public void setStyleDetail(StyleDetail styleDetail) {
		this.styleDetail = styleDetail;
	}

	public Trim getTrim() {
		return trim;
	}

	public void setTrim(Trim trim) {
		this.trim = trim;
	}

	public ActionNotes getActionNotes() {
		return actionNotes;
	}

	public void setActionNotes(ActionNotes actionNotes) {
		this.actionNotes = actionNotes;
	}

}
